package com.weiyi.mashangzhu.Roomer.Activity;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import com.google.gson.Gson;
import com.squareup.okhttp.Request;
import com.weiyi.mashangzhu.API.API;
import com.weiyi.mashangzhu.Base.BaseActivity;
import com.weiyi.mashangzhu.R;
import com.weiyi.mashangzhu.Roomer.Adapter.ThemeAdapter;
import com.weiyi.mashangzhu.Roomer.Bean.ActivityBean;
import com.weiyi.mashangzhu.Utils.SPUtils;
import com.weiyi.mashangzhu.Utils.T;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ThemeActivity extends BaseActivity {

    @BindView(R.id.vp)
    ViewPager vp;
    @BindView(R.id.layout)
    LinearLayout layout;
    List<ActivityBean.DataBean> dataBeanList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_theme);
        ButterKnife.bind(this);
        initView();
    }

    //获取全部活动内容
    @Override
    public void initView() {

        OkHttpUtils.post().addParams("token", SPUtils.get(this, "token", "") + "").url(API.BaseApi_R + API.Main_Activity_APi).build().execute(new StringCallback() {
            @Override
            public void onError(Request request, Exception e) {
                T.showShort(ThemeActivity.this, "数据加载出错");
            }

            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                dataBeanList = gson.fromJson(response, ActivityBean.class).getData();
                ThemeAdapter themeAdapter = new ThemeAdapter(ThemeActivity.this, dataBeanList);
                vp.setAdapter(themeAdapter);
                initDots();
            }
        });

        super.initView();

    }

    //给viewpager添加指示器
    private void initDots() {
        final ImageView[] icons;
        icons = new ImageView[dataBeanList.size()];
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(30, 30);
        params.setMargins(0, 30, 30, 0);
        for (int i = 0; i < icons.length; i++) {
            icons[i] = new ImageView(this);
            icons[i].setLayoutParams(params);  //设置图片宽高
            icons[i].setScaleType(ImageView.ScaleType.CENTER_CROP);
            icons[i].setImageResource(R.drawable.shape_tips_unselected); //图片资源
            layout.addView(icons[i]); //动态添加图片
        }
        icons[0].setImageResource(R.drawable.shape_tips_selected);
        vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                for (int i = 0; i < icons.length; i++) {
                    icons[i].setImageResource(R.drawable.shape_tips_unselected);
                }
                icons[position % dataBeanList.size()].setImageResource(R.drawable.shape_tips_selected);
            }


            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @OnClick(R.id.back)
    public void onViewClicked() {
        finish();
    }
}
